//
//  CouchBaseLiteLib.h
//  FMS
//
//  Created by Steven on 10/19/13.
//  Copyright (c) 2013 QGSVN. All rights reserved.
//

#import "AFHTTPClient.h"

@interface CouchBaseLiteLib : NSObject{
    NSMutableArray* _replications;
    CBLDatabase *_database;
}


@property (strong, nonatomic) CBLDatabase *database;
@property (strong, nonatomic) NSURL* syncURL;

@property (nonatomic) bool continuous;
@property (nonatomic, readonly) unsigned completed, total;
@property (nonatomic, readonly) float progress;
@property (nonatomic, readonly) bool active;
@property (nonatomic, readonly) CBLReplicationMode mode;
@property (nonatomic, readonly) NSError* error;

+ (CouchBaseLiteLib *)sharedCouchBaseLite;

- (void) setUsername: (NSString*)username password: (NSString*)password;
- (void) setSyncURL: (NSURL*)url;
- (void) setRemoteDatebaseUrl:(NSString*)_remoteDBUrl authenticateUser:(NSDictionary*)dict;
- (void) syncNow;
- (void) deleteLocalDatabaseWithParent:(id)_parent;

- (NSError*) updateDoc:(CBLDocument*)doc withContent:(BOOL (^)(NSMutableDictionary* dict))block countTimer:(int)cnt;

- (void) initAllViews;

#pragma mark user methods

- (int) countUser;
- (NSMutableDictionary*) getUserByEmail:(NSString*)email;
- (NSError *) updateUserAccount:(NSString*)accountAuth newDict:(NSMutableDictionary*)dict;

#pragma mark role methods
- (NSMutableDictionary *)getPermissionByRoleName:(NSString *)roleName;
- (NSMutableArray*) listAllRole;

+ (CBLDocument*) getDocumentById:(NSString*)_documentId;
+ (BOOL) addNewRoom:(CBLDatabase*)_database inputDict:(NSMutableDictionary*)_inputDict;
+ (BOOL) updateRoom:(CBLDocument*)_document inputDict:(NSMutableDictionary*)_inputDict;

+ (NSMutableArray*) listRoomFrom:(int)from numberRow:(int)count;
+ (NSMutableArray*) listRoomByContractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listRoomByFilter:(NSMutableDictionary*)_filterDictValue from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listRoomByKey:(NSString*)_searchKey andFilter:(NSMutableDictionary*)_filterDictValue from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listRoomByFilter:(NSMutableDictionary*)_filterDictValue contractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listRoomByKey:(NSString*)_searchKey andFilter:(NSMutableDictionary*)_filterDictValue contractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;

+ (BOOL) addNewEquipment:(CBLDatabase*)_database inputDict:(NSMutableDictionary*)_inputDict;
+ (BOOL) updateEquipment:(CBLDocument*)_document inputDict:(NSMutableDictionary*)_inputDict;
+ (NSMutableArray*) listEquipment;
+ (NSMutableArray*) listEquipmentByFilter:(NSMutableDictionary*)_filterDictValue from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listEquipmentByKey:(NSString*)_searchKey andFilter:(NSMutableDictionary*)_filterDictValue from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listEquipmentByFilter:(NSMutableDictionary*)_filterDictValue contractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listEquipmentByKey:(NSString*)_searchKey andFilter:(NSMutableDictionary*)_filterDictValue contractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;

+ (BOOL) addNewInspection:(NSMutableDictionary*)_inputDict;
+ (NSMutableArray*) listInspection;
+ (NSMutableArray*) listInspectionFrom:(int)from numberRow:(int)count;
+ (NSMutableArray*) listInspectionByContractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listInspectionByItem:(NSString*)_itemId;

+ (NSMutableArray*) listMaintenance;
+ (NSMutableArray*) listMaintenanceFrom:(int)from numberRow:(int)count;

+ (NSMutableArray*) listMaintenanceByType:(int)_maintenanceTypeId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listMaintenanceByType:(int)_maintenanceTypeId contractor:(NSString*)_contractorId from:(int)from numberRow:(int)count;

+ (NSMutableArray*) listMaintenanceByItem:(NSString*)_itemId;
+ (NSMutableArray*) listMaintenanceAndInspectionByItem:(NSString*)_itemId from:(int)from numberRow:(int)count;

#pragma mark Schedule
+ (int) countUnreadSchedulingByItem:(NSMutableDictionary*)_itemDict;
+ (NSMutableArray*) listScheduleByItem:(NSMutableDictionary*)_itemDict from:(int)from numberRow:(int)count;
#pragma mark Document Attachment
+ (NSMutableArray*) listDocumentAttachmentByItem:(NSString*)_itemId;
+ (NSMutableArray*) listItemsByDocument:(NSString*)_documentLink;
+ (NSMutableArray*) listItemsByDocument:(NSString*)_documentLink from:(int)from numberRow:(int)count;
+ (BOOL) checkExistedItemByDocument:(NSString*)_itemId documentLink:(NSString*)_documentLink;
#pragma mark Filter Data
+ (NSMutableArray*) listService;
+ (NSMutableArray*) listCampByService:(NSString*)_serviceId;
+ (NSMutableArray*) listUnitByService:(NSString*)_serviceId andCamp:(NSString*)_campId;
+ (NSMutableArray*) listBuildingByUnit:(NSString*)_unitId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listOtherBuildingByUnit:(NSString*)_unitId from:(int)from numberRow:(int)count;
+ (NSMutableArray*) listFloorsByBuilding:(NSString*)_buildingId;
+ (NSMutableArray*) listSystemByFilter:(NSMutableDictionary*)_filterValueDict;
+ (NSMutableArray*) listDescriptionBySystem:(NSString*)_systemName;

+ (NSMutableDictionary*) getServiceByName:(NSString*)_serviceName;
+ (NSMutableDictionary*) getCampByService:(NSString*)_serviceId andCampName:(NSString*)_campName;
+ (NSMutableDictionary*) getUnitByService:(NSString*)_serviceId andCamp:(NSString*)_campId andUnitName:(NSString*)_unitName;
+ (NSMutableDictionary*) getBuildingByName:(NSString*)_buildingName;
+ (NSMutableDictionary*) getFloorByBuilding:(NSString*)_buildingName andFloorName:(NSString*)_floorName;

#pragma mark Attachment Document
+ (NSMutableArray*) listAttachmentByMaintenance:(NSString*)_maintenanceId andType:(int)_maintenanceType;

- (CBLDocument*) createDocumentWithAttachment:(NSMutableDictionary*)content fileData:(NSData*)fileData error:(NSError **)error;
- (CBLDocument *)createDocument:(NSMutableDictionary *)content error:(NSError **)error;
- (CBLDocument *)updateDocument:(NSMutableDictionary *)content error:(NSError **)error;
- (CBLDocument *)getDocumentById:(NSString *)idDocument;
- (void)deleteDocumentById:(NSString *)idDocument error:(NSError **)error;

#pragma mark USER
+ (NSMutableArray*) listUserByRole:(NSString*)_roleName;
+ (NSMutableArray *) listUserLiasonAndContact:(int)indexFrom numberPage:(int)count;
+ (NSMutableArray *) listUserLiasonAndContactByUser:(NSString*)_userId from:(int)indexFrom numberPage:(int)count;
@end
